package org.budo.netty.servlet.server.handler.servlet;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;

/**
 * @author lmw
 */
public class BudoServletInputStream extends ServletInputStream {
    private static final String UTF_8 = "UTF-8";

    private final ByteArrayInputStream byteArrayInputStream;

    public BudoServletInputStream(String requestBody) {
        try {
            byte[] bytes = requestBody.getBytes(UTF_8);
            this.byteArrayInputStream = new ByteArrayInputStream(bytes);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("#22 Failed to convert characters to bytes using encoding \"" + UTF_8 + "\".");
        }
    }

    public int read() throws IOException {
        return this.byteArrayInputStream.read();
    }

    public int read(byte[] b) throws IOException {
        return this.byteArrayInputStream.read(b);
    }

    public int read(byte[] b, int off, int len) throws IOException {
        return this.byteArrayInputStream.read(b, off, len);
    }

    public boolean markSupported() {
        return this.byteArrayInputStream.markSupported();
    }

    public void mark(int readlimit) {
        this.byteArrayInputStream.mark(readlimit);
    }

    public long skip(long n) throws IOException {
        return this.byteArrayInputStream.skip(n);
    }

    public void reset() throws IOException {
        this.byteArrayInputStream.reset();
    }

    public void close() throws IOException {
        this.byteArrayInputStream.close();
    }

    @Override
    public boolean isFinished() {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean isReady() {
        throw new UnsupportedOperationException();
    }

    @Override
    public void setReadListener(ReadListener readListener) {
        throw new UnsupportedOperationException();
    }
}